package a66weding.com.jiehuntong.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.application.DataWareHourse;
import a66weding.com.jiehuntong.application.GlobalApplication;
import a66weding.com.jiehuntong.beans.CommonResultBean;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.common.api.Url;
import a66weding.com.jiehuntong.common.utils.CUtils;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;

/**
 * 旧的手机验证码界面
 */
public class MeSetBindnumActivity extends MeBaseActivity implements View.OnClickListener , CompoundButton.OnCheckedChangeListener, OnNetResultListener {
    private LinearLayout mBackLay;
    private TextView mBackContentTv;
    private TextView mHeaderTitleTv;
    private TextView mGetverficodeTv;
    private RelativeLayout mHeaderRightRlay;
    private Button mFinishBtn;
    private EditText mOldCodeEt;
    private String oldCode;
    private String verificationCode;
    private TextView mTimeTv;
    private int recLen = 60;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_me_set_bindnum);
        ((GlobalApplication)getApplication()).getActivityManager().addActivity("MeSetBindnumActivity",this); //全局的Activity的管理类
        ((GlobalApplication)getApplication()).getMeActivityManager().addActivity("MeSetBindnumActivity",this);//获取到我的Acivity管理类,向里添加

        initview();
    }

    private void initview() {
        mBackLay = (LinearLayout) findViewById(R.id.llay_header_back);
        mBackContentTv = (TextView) findViewById(R.id.tv_header_backcontent);
        mHeaderTitleTv = (TextView) findViewById(R.id.tv_header_title);
        mGetverficodeTv = (TextView) findViewById(R.id.tv_register_getverficode);
        mHeaderRightRlay = (RelativeLayout) findViewById(R.id.rlay_header_rightfunc);
        mFinishBtn = (Button) findViewById(R.id.btn_me_finish);
        mOldCodeEt = (EditText) findViewById(R.id.et_register_verification);
        mTimeTv = (TextView) findViewById(R.id.tv_me_time);

        mBackContentTv.setText("返回");
        mHeaderTitleTv.setText("手机验证码");
        mHeaderRightRlay.setVisibility(View.GONE);

        mBackLay.setOnClickListener(this);
        mGetverficodeTv.setOnClickListener(this);
        mFinishBtn.setOnClickListener(this);


    }

    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            recLen--;
            mTimeTv.setText("" + recLen);
            handler.postDelayed(this, 1000);
            if (recLen == 0) {
                mTimeTv.setVisibility(View.GONE);
                mGetverficodeTv.setVisibility(View.VISIBLE);
                recLen = 60;
                mTimeTv.setText("" + recLen);
                handler.removeCallbacks(runnable);
                return;
            }
        }
    };

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.llay_header_back:
                finish();
                break;
            case R.id.tv_register_getverficode: //点击获取验证码,发送网络请求,获取验证码


                String phoneNumber = DataWareHourse.getInstance().getmUser().getData().get(0).getNvc_MobilePhone();
                String realUrl= Url.SURL + "/Wedding/VerificationCode?Token=00&MobileNum=" + phoneNumber ;
                HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.POSTEQUEST, realUrl,
                        this, CommonResultBean.class, RequestCode.VERIFICATIONCODE);
                HttpHandleUtil.executeCommonPost(this,httpRequestBean);

                break;
            case R.id.btn_me_finish: //点击完成按钮进入新手机号绑定界面
                oldCode = mOldCodeEt.getText().toString().trim();
                if(TextUtils.isEmpty(oldCode)) {
                    CUtils.showToast("验证码不能为空");
                    return;
                }
                else if(verificationCode == null) {
                    CUtils.showToast("您还没有获取验证码,请先获取验证码");
                    return;
                }
                else if(!(verificationCode.equals(oldCode))) {
                    CUtils.showToast("您输入的验证码有误,请重新输入");
                    return;
                }
                intent.setClass(MeSetBindnumActivity.this,MeNewAlterBindPhoneActivity.class);
                startActivity(intent);
                break;

        }
    }


    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    }

    @Override
    public void onSucess(SuccessBean sucessbean) {
        Object data = sucessbean.getData();
        if(data instanceof CommonResultBean){
            CommonResultBean commonData= (CommonResultBean) data;
            if ("0000".equals(commonData.getResCode())){
                handler.removeCallbacks(runnable);
                mGetverficodeTv.setVisibility(View.GONE);
                mTimeTv.setVisibility(View.VISIBLE);
                handler.postDelayed(runnable,1000);
                Toast.makeText(MeSetBindnumActivity.this, "验证码已发送到您的手机,请注意查收", Toast.LENGTH_SHORT).show();
                verificationCode = commonData.getData();
            }

        }
    }

    @Override
    public void onError(ErrorBean errorBean) {
        Toast.makeText(MeSetBindnumActivity.this, "生成验证码失败", Toast.LENGTH_SHORT).show();
    }
}
